<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\validate\inspection;

use think\Validate;

class InspectionLogValidate extends Validate
{
    protected $rule = [
        'id' => 'require|number',
        'status|巡检情况' => 'require|number',
        'site_id|巡检位置' => 'require|number',
        'cate_name|巡检类目' => 'require',
    ];

    public function sceneAdd()
    {
        return $this->remove('id', 'require');
    }

    public function sceneEdit()
    {
        return $this->remove('id', 'require');
    }

    public function sceneDel()
    {
        return $this->only(['ids'])->append('ids', 'require|array');
    }

    public function sceneBatchAdd()
    {
        return $this->only([
            'site_id',
            'data'
        ])->append([
            'data|巡检项目' => 'require|array|checkData'
        ]);
    }


    public function checkData($value)
    {
        if (!is_array($value)) {
            return '巡检项目数据错误';
        }

        foreach ($value as $k => $v) {
            if (!isset($v['status']) || !in_array($v['status'], [1, 2])) {
                return '请选择巡检情况';
            }
            // 异常时必须输入原因和上传图片
            if ($v['status'] == 2) {
                if (!isset($v['remark']) || !$v['remark']) {
                    return '请输入备注说明';
                }

                if (!isset($v['pictures']) || !is_array($v['pictures']) || !$v['pictures']) {
                    return '请上传图片';
                }
            }
        }

        return true;
    }

}